/**
 *
 */
package com.neiquan.secondhand.controller;

import static com.neiquan.secondhand.util.NqConstant.SESSION_USER;
import static com.neiquan.secondhand.util.NqConstant.SESSION_USER_USERNAME;
import com.neiquan.backstage.core.Factor;
import com.neiquan.backstage.core.bsc.impl.BasePersistentServiceImpl;
import com.neiquan.secondhand.po.User;

import com.neiquan.secondhand.util.NqStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 登录
 * @author luochenxi
 * @date 2016-6-12 下午01:33:05
 * @descript 约伴control控制
 */
@Controller
public class LoginController extends BasePersistentServiceImpl{

	@Autowired
	protected HttpSession session;

	@RequestMapping(value="/login")
	public String Login(){
		return "login";
	}

	/**
	 * 登录
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/logined",method = RequestMethod.POST)
	public String LogIned(HttpServletRequest request){
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		User user =getUnique(User.class, Factor.create("account", Factor.C.Eq, username),Factor.create("password", Factor.C.Eq, NqStringUtils.md5Base64(password)));
		if(user!=null){
			session.setAttribute(SESSION_USER, user);
			session.setAttribute(SESSION_USER_USERNAME, user.getAccount());
			return "success";
		}else {
			return "fail";
		}

	}


	/**
	 * 退出登录
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/logout")
	public String LogOut(){
		session.invalidate();
		return "redirect:login.do";
	}


}

